package com.bocloud.cmp.server.controller.app.resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.app.resource.MonitorConnfig;
import com.bocloud.cmp.entity.app.resource.OpenshiftMonitor;
import com.bocloud.cmp.interfaces.app.resource.OpenshiftMonitorService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

/**
 * openshift监控server的控制层
 * 
 * @author Zaney
 * @data:2017年3月31日
 * @describe:
 */
@RestController
@RequestMapping("/openshift/monitor")
public class OpenshiftMonitorController {
	private final OpenshiftMonitorService openshiftMonitorService;
	private final MonitorConnfig monitorConnfig;

	@Autowired
	public OpenshiftMonitorController(OpenshiftMonitorService openshiftMonitorService, MonitorConnfig monitorConnfig) {
		super();
		this.openshiftMonitorService = openshiftMonitorService;
		this.monitorConnfig = monitorConnfig;
	}
	/**
	 * pod/node监控
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/kubernetes/monitor", method = { RequestMethod.GET })
	public BsmResult kubernetesMonitor(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			OpenshiftMonitor monitor = JSONObject.parseObject(object.toJSONString(), OpenshiftMonitor.class);
			monitor.setDomainName(monitorConnfig.getMonitorUrl());
			return openshiftMonitorService.getKubernetesMonitor(monitor);
		} else {
			return ResultTools.formatErrResult();
		}
	}
	/**
	 * application监控
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/app/monitor", method = { RequestMethod.GET })
	public BsmResult appMonitor(@RequestParam(value = Common.PARAMS, required = true) String params) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			OpenshiftMonitor monitor = JSONObject.parseObject(object.toJSONString(), OpenshiftMonitor.class);
			monitor.setDomainName(monitorConnfig.getMonitorUrl());
			return openshiftMonitorService.getAppMonitor(monitor);
		} else {
			return ResultTools.formatErrResult();
		}
	}

}
